﻿//JumbotCms_4.1.0.0
using System;
using System.Data;
using System.Web;
using Jumbot.Common;
namespace JumbotCms.Extends.Link
{
    public partial class _ajax : JumbotCms.API.AdminCenter
    {
        private string _operType = string.Empty;
        private string _response = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            Server.ScriptTimeout = 8;//脚本过期时间
            this._operType = q("oper");
            switch (this._operType)
            {
                case "ajaxGetList":
                    ajaxGetList();
                    break;
                case "ajaxBatchOper":
                    ajaxBatchOper();
                    break;
                case "checkname":
                    ajaxCheckName();
                    break;
                case "updatefore":
                    ajaxUpdateFore();
                    break;
                default:
                    DefaultResponse();
                    break;
            }

            Response.Write(this._response);
        }

        private void DefaultResponse()
        {
            this._response = "{result :\"未知操作\"}";
        }
        private void ajaxCheckName()
        {
            this._response = JsonResult(1, "可以重复");
        }
        private void ajaxGetList()
        {
            Admin_Load("0000", "json");
            string ccid = Str2Str(q("ccid"));
            string id = Str2Str(q("id"));
            int PSize = Str2Int(q("pagesize"), 0) == 0 ? 15 : Str2Int(q("pagesize"), 0);
            int page = Int_ThisPage();
            int countNum = 0;

            string sqlStr = "";
            string whereStr = "[Id]>0";
            doh.Reset();
            doh.ConditionExpress = whereStr;
            countNum = doh.Count("jcms_extends_link");
            sqlStr = Jumbot.Common.staticFunction.GetSql("Id,Title,Url,OrderNum,[State]", "jcms_extends_link", "Id", PSize, page, "desc", whereStr);
            doh.Reset();
            doh.SqlCmd = sqlStr;
            DataTable dt = doh.GetDataTable();
            this._response = "{result :\"1\"," +
                "returnval :\"操作成功\"," +
                "pagerbar :\"" + Jumbot.Common.HtmlPager.GetPageBar(3, "js", 2, countNum, PSize, page, "javascript:ajaxList(<#page#>);") + "\"," +
                Jumbot.Common.JsonHelp.DtToSON(dt) +
                "}";
            dt.Clear();
            dt.Dispose();
        }
        /// <summary>
        /// 执行批量操作
        /// </summary>
        /// <param name="oper"></param>
        /// <param name="ids"></param>
        private void ajaxBatchOper()
        {
            Admin_Load("0000", "json");
            string act = q("act");
            string ids = f("ids");
            string[] idValue;
            idValue = ids.Split(',');
            string ClassId = string.Empty;
            if (act == "pass")
            {
                for (int i = 0; i < idValue.Length; i++)
                {
                    doh.Reset();
                    doh.ConditionExpress = "id=@id";
                    doh.AddConditionParameter("@id", idValue[i]);
                    doh.AddFieldItem("State", 1);
                    doh.Update("jcms_extends_link");
                }
            }
            else if (act == "nopass")
            {
                for (int i = 0; i < idValue.Length; i++)
                {
                    doh.Reset();
                    doh.ConditionExpress = "id=@id";
                    doh.AddConditionParameter("@id", idValue[i]);
                    doh.AddFieldItem("State", 0);
                    doh.Update("jcms_extends_link");
                }
            }
            else//均为删除
            {
                for (int i = 0; i < idValue.Length; i++)
                {
                    doh.Reset();
                    doh.ConditionExpress = "id=@id";
                    doh.AddConditionParameter("@id", idValue[i]);
                    doh.Delete("jcms_extends_link");
                }
            }
            this._response = JsonResult(1, "操作成功");
        }
        private void ajaxUpdateFore()
        {
            Admin_Load("0000", "json");
            string TempStr = "var $$_JSON_PluginLink = {";
            doh.Reset();
            doh.SqlCmd = "SELECT [Id],[Title],[Style],[Url],[Info],[ImgPath] FROM [jcms_extends_link] WHERE [State]=1 ORDER BY Style Desc,OrderNum Desc,Id Desc";
            DataTable dtLink = doh.GetDataTable();
            TempStr += "recordcount: " + dtLink.Rows.Count + ", " +
                "table: [";
            if (dtLink.Rows.Count > 0)
            {
                for (int i = 0; i < dtLink.Rows.Count; i++)
                {
                    if (i > 0) TempStr += ",";
                    TempStr += "{" +
                        "id: " + i + ", " +
                        "type: '" + dtLink.Rows[i]["Style"].ToString() + "', " +
                        "title: '" + dtLink.Rows[i]["Title"].ToString() + "', " +
                        "url: '" + dtLink.Rows[i]["Url"].ToString() + "', " +
                        "info: '" + dtLink.Rows[i]["Info"].ToString() + "', " +
                        "imgpath: '" + dtLink.Rows[i]["ImgPath"].ToString() + "'" +
                        "}";
                }
            }
            TempStr += "]";
            TempStr += "}";
            dtLink.Clear();
            dtLink.Dispose();
            Jumbot.Common.DirFile.SaveFile(TempStr, "~/_data/json/p_link.js");
            this._response = JsonResult(1, "更新完成");
        }
    }
}
